<!--
 * @Descripttion: 
 * @version: 
 * @Author: 
 * @Date: 2019-10-19 20:51:45
 -->
<template>
  <DictionaryTypeView :tableData="tableData"
                      :page="page"
                      :selectList="selectList"
                      @changePage="changePage"
                      @sizeChange="sizeChange"
                      @deleteTableList="deleteTableList"
                      @onSubmitForm="onSubmitForm"
                      @onSubmitWriteForm="onSubmitWriteForm"></DictionaryTypeView>
</template>
<script>
import DictionaryTypeView from './DictionaryTypeView'
import moreUseApi from '@/httpUtils/MoreUse/moreUse'
export default {
  name: 'DictionaryTypeLogic',
  components: {
    DictionaryTypeView
  },
  mounted () {
    //  根据id获取列表信息
    this.getTableList()

    //  获取所有参数的下拉列表
    moreUseApi.queryDictList().then(res => {
      this.selectList = res.data.data
    })
  },
  data () {
    return {
      page: {
        pageNum: 1,//  当前页
        pageSize: 5,//  每页几条
        total: 2,
        typeCode: ''
      },
      tableData: [],
      selectList: []
    }
  },
  watch: {
    page: {
      handler (newName, oldName) {
        moreUseApi.findAllDictInfoByType(this.page).then(res => {
          this.tableData = res.data.data.list
          this.page.pageNum = res.data.data.pageNum
          this.page.pageSize = res.data.data.pageSize
          this.page.total = res.data.data.total
        })
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    getTableList () {
      if (this.$route.query) {
        this.page.typeCode = this.$route.query.id
      }
    },
    //  点击分页按钮
    changePage (pageNum) {
      console.log(pageNum)
      this.page.pageNum = pageNum
    },
    //  切换页数
    sizeChange (pageSize) {
      this.page.pageSize = pageSize
    },
    //  点击删除按钮
    deleteTableList (data) {
      console.log(data)
      moreUseApi.deleteDictByDictId({ dictId: data.dictId }).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除成功')
          moreUseApi.findAllDictInfoByType(this.page).then(res => {
            this.tableData = res.data.data.list
            this.page.pageNum = res.data.data.pageNum
            this.page.pageSize = res.data.data.pageSize
            this.page.total = res.data.data.total
          })
        } else {
          this.$message.error('服务器错误')
        }
      })
    },
    //  表单点击确定按钮
    onSubmitForm (data) {
      data.typeCode = this.$route.query.id
      moreUseApi.addDictInfo(data).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          moreUseApi.findAllDictInfoByType(this.page).then(res => {
            this.tableData = res.data.data.list
            this.page.pageNum = res.data.data.pageNum
            this.page.pageSize = res.data.data.pageSize
            this.page.total = res.data.data.total
          })
        } else {
          this.$message.error('服务器错误')
        }
      })
    },
    //  修改界面点击确定
    onSubmitWriteForm (data) {
      console.log(data)
      let obj = {
        dictName: data.dictName,
        dictId: data.dictId,
        parentId: this.$route.query.id
      }
      moreUseApi.updateDictTypeByDictId(obj).then(res => {
        if (res.data.code === 200) {
          this.$message.success(res.data.message)
          moreUseApi.findAllDictInfoByType(this.page).then(res => {
            this.tableData = res.data.data.list
            this.page.pageNum = res.data.data.pageNum
            this.page.pageSize = res.data.data.pageSize
            this.page.total = res.data.data.total
          })
        } else {
          this.$message.error('服务器错误')
        }
      })
    }
  }
}
</script>
<style scoped>
@import url("DictionaryType.less");
</style>